package com.prothink.controller;

import com.prothink.entity.RequestData;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : Upload  //类名
 * @Description : 视频的图片上传  //描述
 * @Author : Zhang //作者
 * @Date: 12/14  9:46
 */
@CrossOrigin
@RequestMapping("/upload")
@RestController
public class Upload {

    @RequestMapping("/addimg")
    public RequestData addimg(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()){
            System.out.println("上传失败，请选择文件");
            return null;
        }
        //获取文件原始名
        String originalFilename = file.getOriginalFilename();

        try {
            File newFile = new File("E:/studyworkspace/siyun_vue/public/images/" + originalFilename);
            System.out.println("文件路径："+newFile.getAbsolutePath());
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map = new HashMap<>();
        map.put("url",originalFilename);
        return RequestData.buildSuccess(map);
    }


    @RequestMapping("/addvideo")
    public RequestData addvideo(@RequestParam("file") MultipartFile file) {
        //获取文件原始名
        String originalFilename = file.getOriginalFilename();

        try {
            File newFile = new File("E:/studyworkspace/siyun_vue/public/video/" + originalFilename);
            System.out.println("文件路径："+newFile.getAbsolutePath());
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map = new HashMap<>();
        map.put("courseUrl",originalFilename);
        return RequestData.buildSuccess(map);
    }
}
